#include "DbCRUDService.h"
#include <iostream> // 用于示例日志输出，可选
#include <grpcpp/grpcpp.h> // 确保包含 gRPC 状态码等
#include "../../tools/DbCRUD/DbCreate.h"

using grpc::Status;
using grpc::StatusCode;

// --- CRUD 操作接口 (供 heartwall 后端同步数据) ---

// --- 创建 (Create) ---
Status DbCRUDService::CreateUser(
    ServerContext* context,
    const CreateUserRequest* request,
    CreateUserResponse* response) {
    
    const ::graph::UserNode& user_node = request->user();
    std::string error_message;

    if (DbCreate::CreateUser(user_node, error_message)) {
        response->set_success(true);
        std::cout << "Successfully created user with ID: " << user_node.id() << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to create user with ID: " << user_node.id() 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::CreateList(
    ServerContext* context,
    const CreateListRequest* request,
    CreateListResponse* response) {
    
    const ::graph::ListNode& list_node = request->list();
    std::string error_message;

    if (DbCreate::CreateList(list_node, error_message)) {
        response->set_success(true);
        std::cout << "Successfully created list with ID: " << list_node.id() << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to create list with ID: " << list_node.id() 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::CreateTodo(
    ServerContext* context,
    const CreateTodoRequest* request,
    CreateTodoResponse* response) {
    
    const ::graph::TodoNode& todo_node = request->todo();
    std::string error_message;

    if (DbCreate::CreateTodo(todo_node, error_message)) {
        response->set_success(true);
        std::cout << "Successfully created todo with ID: " << todo_node.id() << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to create todo with ID: " << todo_node.id() 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::CreateComment(
    ServerContext* context,
    const CreateCommentRequest* request,
    CreateCommentResponse* response) {
    
    const ::graph::CommentNode& comment_node = request->comment();
    std::string error_message;

    if (DbCreate::CreateComment(comment_node, error_message)) {
        response->set_success(true);
        std::cout << "Successfully created comment with ID: " << comment_node.id() << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to create comment with ID: " << comment_node.id() 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::CreateEntity(
    ServerContext* context,
    const CreateEntityRequest* request,
    CreateEntityResponse* response) {
    
    const ::graph::EntityNode& entity_node = request->entity();
    std::string error_message;

    if (DbCreate::CreateEntity(entity_node, error_message)) {
        response->set_success(true);
        std::cout << "Successfully created entity with ID: " << entity_node.id() << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to create entity with ID: " << entity_node.id() 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::CreateTimeSlot(
    ServerContext* context,
    const CreateTimeSlotRequest* request,
    CreateTimeSlotResponse* response) {
    
    const ::graph::TimeSlotNode& time_slot_node = request->time_slot();
    std::string error_message;

    if (DbCreate::CreateTimeSlot(time_slot_node, error_message)) {
        response->set_success(true);
        std::cout << "Successfully created time slot for date: " << time_slot_node.date() << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to create time slot for date: " << time_slot_node.date() 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}

Status DbCRUDService::CreateTrendScore(
    ServerContext* context,
    const CreateTrendScoreRequest* request,
    CreateTrendScoreResponse* response) {
    
    const ::graph::TrendScoreNode& trend_score_node = request->trend_score();
    std::string error_message;

    if (DbCreate::CreateTrendScore(trend_score_node, error_message)) {
        response->set_success(true);
        std::cout << "Successfully created trend score for list ID: " << trend_score_node.list_id() 
                  << " on date: " << trend_score_node.date() << std::endl;
    } else {
        response->set_success(false);
        response->set_error_message(error_message);
        std::cerr << "Failed to create trend score for list ID: " << trend_score_node.list_id() 
                  << " on date: " << trend_score_node.date() 
                  << ". Error: " << error_message << std::endl;
    }

    return Status::OK;
}
